package com.ruoyi.system.outto.goods.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.model.domain.ArtRec;
import com.ruoyi.system.model.domain.GameAttribute;
import com.ruoyi.system.model.domain.GoodsInfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.model.mapper.SuprAuthMapper;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.goods.NEWWUYOUAPI;
import com.ruoyi.system.outto.goods.NEWYXGAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/5 16:19
 */
@Slf4j
@Service
public class NEWYXGAPIImpl implements NEWYXGAPI {

    @Resource
    private InfoImgMapper infoImgMapper;
    @Resource
    private SuprAuthMapper suprAuthMapper;
    @Resource
    private GoodsInfoImgMapper goodsInfoImgMapper;
    @Resource
    private GameAttributeMapper gameAttributeMapper;
    @Resource
    private ArtRecMapper artRecMapper;
    @Resource
    private CommonApi commonApi;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;

    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, "add", salePartner);
        Map<String, Object> result = new HashMap<>();
        String response = HttpUtils.doPostForm(salePartner.getOther(), params, null);
        log.info("游戏阁添加接口请求参数={}", JSONObject.toJSONString(params));
        log.info("游戏阁添加接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!Objects.isNull(resultJson)) {
            if (!"true".equals(resultJson.getString("succ"))) {
                result.put("code", 500);
            } else {
                result.put("code", 200);
                ArtRec artRecreq = new ArtRec();
                artRecreq.setConfig(salePartner.getId());
                artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
                if (CollectionUtils.isNotEmpty(artRecs)) {
                    artRecs.get(0).setOutid(String.valueOf(goodsInfoAndAttrValueDto.getId()));
                    artRecMapper.updateArtRec(artRecs.get(0));
                } else {
                    artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                    artRecreq.setCreateId(goodsInfoAndAttrValueDto.getCreateId());
                    artRecreq.setType(LCConstant.interAdd);
                    artRecreq.setConfig(salePartner.getId());
                    artRecreq.setSaleCode(salePartner.getSaleName());
                    artRecreq.setOutid(String.valueOf(goodsInfoAndAttrValueDto.getId()));
                    artRecMapper.insertArtRec(artRecreq);
                }
            }
            result.put("msg", resultJson.getString("msg"));
        } else {
            result.put("code", 500);
            result.put("msg", "添加失败");
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, "update", salePartner);
        Map<String, Object> result = new HashMap<>();
        String response = HttpUtils.doPostForm(salePartner.getOther(), params, null);
        log.info("游戏阁编辑接口请求参数={}", JSONObject.toJSONString(params));
        log.info("游戏阁编辑接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!Objects.isNull(resultJson)) {
            if (!"true".equals(resultJson.getString("succ"))) {
                result.put("code", 500);
            } else {
                result.put("code", 200);
            }
            result.put("msg", resultJson.getString("msg"));
        } else {
            result.put("code", 500);
            result.put("msg", "编辑失败");
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        return AjaxResult.success(result);
    }


    private Map<String, Object> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, String type, SalePartner salePartner) {
        Map<String, Object> params = new HashMap<>();
        params.put("key", salePartner.getSaleKey());
        params.put("merchant_no", salePartner.getTwoOther());
        params.put("pid", goodsInfoAndAttrValueDto.getId());
        params.put("title", "【"+descTitle+"_"+goodsInfoAndAttrValueDto.getId()+"】"+ goodsInfoAndAttrValueDto.getDes());

        BigDecimal bigDecimal = commonApi.addPrice(goodsInfoAndAttrValueDto.getPrice(), salePartner.getSeleep());
        commonApi.setPrice(bigDecimal, goodsInfoAndAttrValueDto.getId(), salePartner);
        params.put("price", bigDecimal);

        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        List<GoodsInfoImg> infoImgs = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
        String pic = "";
        if (Objects.nonNull(goodsInfoAndAttrValueDto.getTitleImg())) {
            pic = goodsInfoAndAttrValueDto.getTitleImg() + ",";
        }
        if (!CollectionUtils.isEmpty(infoImgs)) {
            pic = pic + infoImgs.stream().map(i -> i.getImgUrl()).collect(Collectors.joining(","));
        } else {
            pic = pic + LCConstant.defaultImg;
        }
        params.put("pic", pic);
        params.put("paiwei", "可排位");
        params.put("account", descTitle + goodsInfoAndAttrValueDto.getId());
        params.put("type", type);
        GameAttribute gameAttribute = null;
        if ("王者荣耀".equals(goodsInfoAndAttrValueDto.getGameName())) {
            if(StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF16())){
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF16()));
            params.put("gameid", "3");
            //游戏大区
            String youxidaqu = youxiqufu(3);
            JSONObject wzresult = JSONObject.parseObject(youxidaqu);
            JSONArray options = JSONObject.parseArray((wzresult.get("arealist")).toString());
            for (Object o : options) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(o));
                if (String.valueOf(jsonObject1.get("name")).contains(goodsInfoAndAttrValueDto.getSystemName() + goodsInfoAndAttrValueDto.getCarrierName())) {
                    params.put("serverid", Integer.parseInt(jsonObject1.getString("id")));
                }
            }
            //扩展属性
            List<String> dcpf = new ArrayList<>();
            List<String> xyzc = new ArrayList<>();
            String respones = kuozhanshuxing(3);
            JSONObject kuozhanshuxingResult = JSONObject.parseObject(respones);
            JSONArray kuozhanshuxing = JSONObject.parseArray(kuozhanshuxingResult.get("data").toString());
            String pfIds = "";

            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF9());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF10());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF11());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF12());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF15());
            List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
            String guizujifen = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF4())){
                guizujifen = goodsInfoAndAttrValueDto.getF4();
            }else {
                guizujifen = "0";
            }
            String guizudengji = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())){
                guizudengji = goodsInfoAndAttrValueDto.getF2();
            }else {
                guizudengji = "V1";
            }
            String hours = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF7())){
                hours = goodsInfoAndAttrValueDto.getF7();
            }else {
                hours = "0";
            }
            String skins = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF8())){
                skins = goodsInfoAndAttrValueDto.getF8();
            }else {
                skins = "0";
            }
            String ryshuijing = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF6())){
                ryshuijing = goodsInfoAndAttrValueDto.getF6();
            }else {
                ryshuijing = "0";
            }
            String yingdi = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF17())){
                yingdi = goodsInfoAndAttrValueDto.getF17();
            }else {
                yingdi = "123456";
            }
            String duanwei = "";
            for (Object o : kuozhanshuxing) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(o));
                if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())) {
                    GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
                    if (jsonObject1.getString("propertyname").equals("段位")) {
                        JSONArray jsonArray = JSONObject.parseArray((jsonObject1.get("options")).toString());
                        for (Object p : jsonArray) {
                            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(p));
                            if(jsonObject.getString("key").contains(gameAttribute1.getFieldName())){
                                duanwei = jsonObject.getString("value");
                                break;
                            }
                        }
                    }
                }else{
                    duanwei = "未定级";
                }
                if (jsonObject1.get("propertyname").equals("星元皮肤")) {
                    //星元皮肤
                    JSONArray diancangpifuArray = JSONObject.parseArray((jsonObject1.get("options")).toString());
                    for (GameAttribute gameAttribute1 : gameAttributesss) {
                        for (Object a : diancangpifuArray) {
                            JSONObject diancangpifu = JSONObject.parseObject(JSONObject.toJSONString(a));
                            if (gameAttribute1.getFieldName().contains(diancangpifu.getString("key"))) {
                                xyzc.add(diancangpifu.getString("value"));
                            }
                        }
                    }
                }
                if (jsonObject1.get("propertyname").equals("荣耀典藏")) {
                    //荣耀典藏
                    JSONArray diancangpifuArray = JSONObject.parseArray((jsonObject1.get("options")).toString());
                    for (GameAttribute gameAttribute1 : gameAttributesss) {
                        for (Object a : diancangpifuArray) {
                            JSONObject diancangpifu = JSONObject.parseObject(JSONObject.toJSONString(a));
                            if (gameAttribute1.getFieldName().contains(diancangpifu.getString("key"))) {
                                dcpf.add(diancangpifu.getString("value"));
                            }
                        }
                    }
                }
                String pawei = "wzguizujifen="+guizujifen+"|wzlevel="+guizudengji+"|wzyxcount="+hours+"|wzpfcount="+skins+"|wzsjcount="+ryshuijing+"|wzydcount="+yingdi+"|wzduanwei="+duanwei;
                String optionss = pawei;
                if (StringUtils.isNotEmpty(xyzc)) {
                    String join = StringUtils.join(xyzc, ",");
                    String remenshizhuang = "xingyuanpifu=" + join;
                    optionss += "|" + remenshizhuang;
                }
                if (StringUtils.isNotEmpty(dcpf)) {
                    String join1 = StringUtils.join(dcpf, ",");
                    String xingyuanzhencang = "rengyaodiancang=" + join1;
                    optionss += "|" + xingyuanzhencang;
                }
                params.put("options", optionss.replace("null", ""));
            }

        }
        else if ("和平精英".equals(goodsInfoAndAttrValueDto.getGameName())) {
            if(StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF10())){
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
            params.put("gameid", "5");
            //游戏大区
            String response = youxiqufu(5);
            JSONObject wzresult = JSONObject.parseObject(response);
            JSONArray options = JSONObject.parseArray((wzresult.get("arealist")).toString());
            for (Object o : options) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(o));
                if (jsonObject1.get("name").toString().contains(goodsInfoAndAttrValueDto.getSystemName() + goodsInfoAndAttrValueDto.getCarrierName())) {
                    params.put("serverid", Integer.parseInt(jsonObject1.getString("id")));
                }
            }
            //扩展属性
            List<String> rmsz = new ArrayList<>();
            List<String> xyqp = new ArrayList<>();
            List<String> xyzj = new ArrayList<>();
            String responsee = kuozhanshuxing(5);
            JSONObject resultJson1 = JSONObject.parseObject(responsee);
            JSONArray array = JSONArray.parseArray(resultJson1.get("data").toString());
            List<GameAttribute> rmszAttributesss = getGameAttributs(goodsInfoAndAttrValueDto.getF5());
            List<GameAttribute> xyqpAttributesss = getGameAttributs(goodsInfoAndAttrValueDto.getF1());
            List<GameAttribute> xyzjAttributesss = getGameAttributs(goodsInfoAndAttrValueDto.getF2());
            String duanwei = "";
            GameAttribute gameAttribute1 = null;
            String fieldName = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF15())){
                gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF15()));
                if(gameAttribute1.getFieldName().contains("皇冠")){
                    fieldName = "光冠";
                }else{
                    fieldName = gameAttribute1.getFieldName();
                }
            }else{
                duanwei = "未定级";
            }
            String taozhuang = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF8())){
                taozhuang = goodsInfoAndAttrValueDto.getF8();
            }else{
                taozhuang = "0";
            }
            String qiangpi = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF9())){
                qiangpi = goodsInfoAndAttrValueDto.getF9();
            }else{
                qiangpi = "0";
            }
            String zaiju = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF7())){
                zaiju = goodsInfoAndAttrValueDto.getF7();
            }else{
                zaiju = "0";
            }
            String feixingqi = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())){
                feixingqi = String.valueOf(goodsInfoAndAttrValueDto.getF3().split(",").length);
            }else{
                feixingqi = "0";
            }
            String xiyouzaiju = "";
            for (Object o : array) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(o));
                if (jsonObject1.getString("propertyname").equals("段位")) {
                    JSONArray jsonArray = JSONArray.parseArray(jsonObject1.getString("options"));
                    for(Object p:jsonArray){
                        if(JSONObject.parseObject(p.toString()).getString("key").contains(fieldName)){
                            duanwei = JSONObject.parseObject(p.toString()).getString("key");
                        }
                    }
                }
                if(jsonObject1.getString("propertyname").equals("稀有载具")){
                    JSONArray jsonArray = JSONArray.parseArray(jsonObject1.getString("options"));
                    for(Object p:jsonArray){
                        for(GameAttribute gameAttribute2 : xyzjAttributesss){
                            xiyouzaiju = gameAttribute2.getFieldName();
                            if(gameAttribute2.getFieldName().contains("特斯拉3银色")||gameAttribute2.getFieldName().contains("特斯拉X银色")){
                                xiyouzaiju = "银色特斯拉";
                            }else if(gameAttribute2.getFieldName().contains("特斯拉3白色")||gameAttribute2.getFieldName().contains("特斯拉X白色")){
                                xiyouzaiju = "白色特斯拉";
                            }else if(gameAttribute2.getFieldName().contains("特斯拉C荒芜蓝")||gameAttribute2.getFieldName().contains("特斯拉R水滴蓝")){
                                xiyouzaiju = "蓝色特斯拉";
                            }else if(gameAttribute2.getFieldName().contains("特斯拉3紫色")||gameAttribute2.getFieldName().contains("特斯拉X紫色")){
                                xiyouzaiju = "紫色特斯拉";
                            }else if(gameAttribute2.getFieldName().contains("玛莎拉蒂Grecale(幻星粉)")||gameAttribute2.getFieldName().contains("玛莎拉蒂Quattroporte(幻星粉)")
                                    ||gameAttribute2.getFieldName().contains("玛莎拉蒂MC20(幻星粉)")||gameAttribute2.getFieldName().contains("玛莎拉蒂粉色")){
                                xiyouzaiju = "粉色玛莎拉蒂";
                            }else if(gameAttribute2.getFieldName().contains("玛莎拉蒂蓝色")){
                                xiyouzaiju = "蓝色玛莎拉蒂";
                            }else if(gameAttribute2.getFieldName().contains("玛莎拉蒂金色")){
                                xiyouzaiju = "金色玛莎拉蒂";
                            }else if(gameAttribute2.getFieldName().contains("合金龙骨红色")){
                                xiyouzaiju = "红色合金龙骨";
                            }else if(gameAttribute2.getFieldName().contains("合金龙骨蓝色")){
                                xiyouzaiju = "蓝色合金龙骨";
                            }else if(gameAttribute2.getFieldName().contains("合金龙骨绿色")){
                                xiyouzaiju = "绿色合金龙骨";
                            }else if(gameAttribute2.getFieldName().contains("阿斯顿马丁")){
                                xiyouzaiju = "阿斯顿马丁";
                            }
                            if(xiyouzaiju.contains(JSONObject.parseObject(p.toString()).getString("key"))){
                                xyzj.add(JSONObject.parseObject(p.toString()).getString("value"));
                            }
                        }
                    }
                }
                if(jsonObject1.getString("propertyname").equals("优质枪皮")){
                    JSONArray jsonArray = JSONArray.parseArray(jsonObject1.getString("options"));
                    for(Object p:jsonArray){
                        for(GameAttribute gameAttribute2 : xyqpAttributesss){
                            if(gameAttribute2.getFieldName().replace("-","").contains(JSONObject.parseObject(p.toString()).getString("key").replace("-",""))){
                                xyqp.add(JSONObject.parseObject(p.toString()).getString("value"));
                            }
                        }
                    }
                }
                if(jsonObject1.getString("propertyname").equals("稀有套装")){
                    JSONArray jsonArray = JSONArray.parseArray(jsonObject1.getString("options"));
                    for(Object p:jsonArray){
                        for(GameAttribute gameAttribute2 : rmszAttributesss){
                            if(gameAttribute2.getFieldName().replace("-","").contains(JSONObject.parseObject(p.toString()).getString("key").replace("-",""))){
                                rmsz.add(JSONObject.parseObject(p.toString()).getString("value"));
                            }
                        }
                    }
                }
                String pawei = "hpjyduanwei="+duanwei+"|hpjytzcount="+taozhuang+"|hpjyqpcount="+qiangpi+"|hpjyzjcount="+zaiju+"|hpjyfxcount="+feixingqi;
                String optionss = pawei;
                if (StringUtils.isNotEmpty(xyzj)) {
                    String join2 = StringUtils.join(xyzj, ",");
                    String xiyouzaijus = "hpjyxyzaiju=" + join2;
                    optionss += "|" + xiyouzaijus;
                }

                if (StringUtils.isNotEmpty(xyqp)) {
                    String join1 = StringUtils.join(xyqp, ",");
                    String xiyouqiangpi = "hpjyyzqp=" + join1;
                    optionss += "|" + xiyouqiangpi;
                }

                if (StringUtils.isNotEmpty(rmsz)) {
                    String join = StringUtils.join(rmsz, ",");
                    String remenshizhuang = "hpjyxytz=" + join;
                    optionss += "|" + remenshizhuang;
                }
                params.put("options", optionss);
            }
        }
        else if ("穿越火线".equals(goodsInfoAndAttrValueDto.getGameName())) {
            if(StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF3())){
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
            params.put("gameid", "1");
            //游戏大区
            String daquresponse = youxiqufu(1);
            JSONObject cfresult = JSONObject.parseObject(daquresponse);
            JSONArray cfdaqu = JSONObject.parseArray((cfresult.get("arealist")).toString());
            for (Object o : cfdaqu) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(o));
                if (goodsInfoAndAttrValueDto.getGroupName().contains(jsonObject1.getString("name"))) {
                    JSONArray jsonArray = jsonObject1.getJSONArray("serverlist");
                    for (Object o1 : jsonArray) {
                        JSONObject jsonObject = JSONObject.parseObject(o1.toString());
                        if (goodsInfoAndAttrValueDto.getServerName().contains(jsonObject.getString("name"))) {
                            params.put("serverid", Integer.parseInt(jsonObject.getString("id")));
                        }
                    }
                }
            }
            String pawei = "paiwei=可排位";
            List<String> huanshen = new ArrayList<>();
            List<String> pifu = new ArrayList<>();
            List<String> rmwq = new ArrayList<>();
            List<String> rmrw = new ArrayList<>();
            String response = kuozhanshuxing(1);
            JSONObject resultJson1 = JSONObject.parseObject(response);
            String pfIds = "";

            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF13());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF14());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF15());
            List<GameAttribute> rmjjAttributesss = getGameAttributs(pfIds);
            String pfIdss = "";

            pfIdss = reIds(pfIdss, goodsInfoAndAttrValueDto.getF16());
            pfIdss = reIds(pfIdss, goodsInfoAndAttrValueDto.getF17());
            pfIdss = reIds(pfIdss, goodsInfoAndAttrValueDto.getF18());
            List<GameAttribute> rmpfAttributesss = getGameAttributs(pfIdss);
            JSONArray youxizhanghao = JSONObject.parseArray(resultJson1.get("data").toString());
            for (Object o : youxizhanghao) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(o));
                if (jsonObject1.get("propertyname").equals("热门狙击")) {
                    //热门狙击
                    JSONArray options = JSONObject.parseArray((jsonObject1.get("options")).toString());
                    for (GameAttribute gameAttribute1 : rmjjAttributesss) {
                        for (Object a : options) {
                            JSONObject remenjuji = JSONObject.parseObject(JSONObject.toJSONString(a));
                            if (gameAttribute1.getFieldName().contains(remenjuji.getString("key"))) {
                                huanshen.add(remenjuji.getString("value"));
                            }
                        }
                    }
                }
                if (jsonObject1.get("propertyname").equals("热门皮肤")) {
                    //热门皮肤
                    String rmpf = "";
                    JSONArray options = JSONObject.parseArray((jsonObject1.get("options")).toString());
                    for (GameAttribute gameAttribute1 : rmpfAttributesss) {
                        if (gameAttribute1.getFieldName().contains("黑骑士-AG-S4冠军典藏")) {
                            rmpf = "黑骑士-S4冠军典藏";
                        }
                        if (gameAttribute1.getFieldName().contains("黑骑士-战神马超")) {
                            rmpf = "黑骑士-马超";
                        }
                        if (StringUtils.isEmpty(rmpf)) {
                            rmpf = gameAttribute1.getFieldName();
                        }
                        for (Object a : options) {
                            JSONObject remenpifu = JSONObject.parseObject(JSONObject.toJSONString(a));
                            if (rmpf.replace("-", "").contains(remenpifu.getString("key").replace("-", "")) || remenpifu.getString("key").replace("-", "").contains(rmpf.replace("-", ""))) {
                                pifu.add(remenpifu.getString("value"));
                            }
                        }
                    }
                }
                if (jsonObject1.get("propertyname").equals("热门武器")) {
                    //热门武器
                    JSONArray options = JSONObject.parseArray((jsonObject1.get("options")).toString());
                    for (GameAttribute gameAttribute1 : rmjjAttributesss) {
                        for (Object a : options) {
                            JSONObject remenwuqi = JSONObject.parseObject(JSONObject.toJSONString(a));
                            if (gameAttribute1.getFieldName().contains(remenwuqi.getString("key"))) {
                                rmwq.add(remenwuqi.getString("value"));
                            }
                        }
                    }
                }
                if (jsonObject1.get("propertyname").equals("热门人物")) {
                    //热门人物
                    List<GameAttribute> rmrwAttributesss = getGameAttributs(goodsInfoAndAttrValueDto.getF19());
                    JSONArray options = JSONObject.parseArray((jsonObject1.get("options")).toString());
                    for (GameAttribute gameAttribute1 : rmrwAttributesss) {
                        for (Object a : options) {
                            JSONObject remenrenwu = JSONObject.parseObject(JSONObject.toJSONString(a));
                            if (gameAttribute1.getFieldName().contains(remenrenwu.getString("key"))) {
                                rmrw.add(remenrenwu.getString("value"));
                            }
                        }
                    }
                }

            }
            String options = pawei;
            if (StringUtils.isNotEmpty(huanshen)) {
                String join = StringUtils.join(huanshen, ",");
                String remenjuji = "remenjuji=" + join;
                options += "|" + remenjuji;
            }
            if (StringUtils.isNotEmpty(pifu)) {
                String join1 = StringUtils.join(pifu, ",");
                String remenpifu = "remenpifu=" + join1;
                options += "|" + remenpifu;
            }
            if (StringUtils.isNotEmpty(rmwq)) {
                String join3 = StringUtils.join(rmwq, ",");
                String remenwuqi = "remenwuqi=" + join3;
                options += "|" + remenwuqi;
            }
            if (StringUtils.isNotEmpty(rmrw)) {
                String join6 = StringUtils.join(rmrw, ",");
                String renmenrenwu = "remenrenwu=" + join6;
                options += "|" + renmenrenwu;
            }
            params.put("options", options.replaceAll(" ", ""));
        } else if ("枪战王者".equals(goodsInfoAndAttrValueDto.getGameName())) {
            if(StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF10())){
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
            //CF手游
            params.put("gameid", "2");
            //游戏大区
            String daquresponse = youxiqufu(2);
            JSONObject wzresult = JSONObject.parseObject(daquresponse);
            JSONArray options = JSONObject.parseArray((wzresult.get("arealist")).toString());
            for (Object o : options) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(o));
                if (jsonObject1.get("name").toString().contains(goodsInfoAndAttrValueDto.getSystemName() + goodsInfoAndAttrValueDto.getCarrierName())) {
                    params.put("serverid", Integer.parseInt(jsonObject1.getString("id")));
                }
            }
        } else if ("原神".equals(goodsInfoAndAttrValueDto.getGameName())) {
            if(StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF20())){
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF20()));
            //原神
            params.put("gameid", "6");
            //游戏大区
            if (goodsInfoAndAttrValueDto.getGroupName().contains("官服")) {
                params.put("serverid", 313);
            } else if (goodsInfoAndAttrValueDto.getGroupName().contains("哔哩哔哩")) {
                params.put("serverid", 314);
            } else {
                params.put("serverid", 315);
            }
            //扩展属性
            List<String> wxjs = new ArrayList<>();
            List<String> wxwq = new ArrayList<>();
            String yskzsx = kuozhanshuxing(6);
            JSONObject wzresults = JSONObject.parseObject(yskzsx);
            JSONArray yskzsxx = JSONObject.parseArray((wzresults.get("data")).toString());
            String keji = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF6())){
                String field = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF6())).getFieldName();
                if (field.contains("不是")){
                    keji = "否";
                }else{
                    keji = "是";
                }
            }else{
                keji = "否";
            }
            for (Object o : yskzsxx) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(o));
                List<GameAttribute> wxjsAttributesss = getGameAttributs(goodsInfoAndAttrValueDto.getF11());
                List<GameAttribute> wxwqAttributesss = getGameAttributs(goodsInfoAndAttrValueDto.getF17());
                if (jsonObject1.getString("propertyname").equals("五星角色")) {
                    JSONArray options1 = JSONObject.parseArray((jsonObject1.getString("options")));
                    for (GameAttribute gameAttribute11 : wxjsAttributesss) {
                        for (Object a : options1) {
                            JSONObject remenjuji = JSONObject.parseObject(JSONObject.toJSONString(a));
                            if (gameAttribute11.getFieldName().contains(remenjuji.getString("key"))) {
                                wxjs.add(remenjuji.getString("value"));
                            }
                        }
                    }
                }
                if (jsonObject1.getString("propertyname").equals("五星武器")) {
                    JSONArray options1 = JSONObject.parseArray((jsonObject1.getString("options")));
                    for (GameAttribute gameAttribute11 : wxwqAttributesss) {
                        for (Object a : options1) {
                            JSONObject remenjuji = JSONObject.parseObject(JSONObject.toJSONString(a));
                            if (gameAttribute11.getFieldName().contains(remenjuji.getString("key"))) {
                                wxwq.add(remenjuji.getString("value"));
                            }
                        }
                    }
                }
            }
            String shifoukejihao = "kejihao="+keji;
            String optionss = shifoukejihao;
            if (StringUtils.isNotEmpty(wxjs)) {
                String join2 = StringUtils.join(wxjs, ",");
                String changzhujuese = "ys_role=" + join2;
                optionss += "|" + changzhujuese;
            }
            if (StringUtils.isNotEmpty(wxwq)) {
                String join3 = StringUtils.join(wxwq, ",");
                String wuxinggjwq = "ys_weapon=" + join3;
                optionss += "|" + wuxinggjwq;
            }
            params.put("options", optionss.replaceAll(" ", ""));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("英雄联盟")){
            if(StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF1())){
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
            params.put("gameid", "9");
            //大区
            String response = youxiqufu(9);
            JSONObject wzresult = JSONObject.parseObject(response);
            JSONArray options = JSONObject.parseArray((wzresult.get("arealist")).toString());
            for(Object o : options){
                if(JSONObject.parseObject(o.toString()).getString("name").contains("电信")||JSONObject.parseObject(o.toString()).getString("name").contains("网通")||JSONObject.parseObject(o.toString()).getString("name").contains("其他")) {
                    JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(o.toString()).getString("serverlist"));
                    for (Object p : jsonArray) {
                        if (goodsInfoAndAttrValueDto.getServerName().contains(JSONObject.parseObject(p.toString()).getString("name"))) {
                            params.put("serverid", JSONObject.parseObject(p.toString()).getString("id"));
                            break;
                        }
                    }
                }
            }
            String hours = "0";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())){
                hours = goodsInfoAndAttrValueDto.getF3();
            }
            String skins = "0";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF4())){
                skins = goodsInfoAndAttrValueDto.getF4();
            }
            String xuancai = "0";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF5())){
                xuancai = goodsInfoAndAttrValueDto.getF5();
            }
            String shenhua = "0";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF18())){
                shenhua = goodsInfoAndAttrValueDto.getF18();
            }
            String xianding = "0";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF19())){
                xianding = goodsInfoAndAttrValueDto.getF19();
            }
            String chuanshuo = "0";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF20())){
                chuanshuo = goodsInfoAndAttrValueDto.getF20();
            }
            String dw = "未定级=未定级,坚韧黑铁=坚韧黑铁,英勇黄铜=英勇黄铜,荣耀黄金=荣耀黄金,华贵白金=华贵白金,璀璨钻石=璀璨钻石,超凡大师=超凡大师,傲世宗师=傲世宗师,最强王者=最强王者";
            String duanwei = "";
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())){
                GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF2()));
                String[] dws = dw.split(",");
                for(String s:dws){
                    if(s.contains(gameAttribute1.getFieldName())){
                        duanwei = s.split("=")[1];
                        break;
                    }
                }
            }else{
                duanwei = "未定级";
            }
            String optionss = "lolyxcount="+hours+"|lolpfcount="+skins+"|lolxccount="+xuancai+"|lolshcount="+shenhua+"|lolxdcount="+xianding+"|lolcscount="+chuanshuo+"|duanwei="+duanwei;
            params.put("options", optionss);
        }else if(goodsInfoAndAttrValueDto.getGameName().equals("逆战")){
            if(StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF1())){
                throw new GlobalException("实名不能为空");
            }
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
            params.put("gameid", "10");
            if(goodsInfoAndAttrValueDto.getGroupName().equals("电信区")){
                params.put("serverid", 276);
            }else {
                params.put("serverid", 275);
            }
            String response = kuozhanshuxing(10);
            String data = JSONObject.parseObject(response).getString("data");
            JSONArray jsonArray = JSONArray.parseArray(data);
            String optionss = "";
            for(Object o : jsonArray) {
                if (JSONObject.parseObject(o.toString()).getString("propertyname").equals("史诗套数量")) {
                    if (StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())) {
                        GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF2()));
                        String options = JSONObject.parseObject(o.toString()).getString("options");
                        JSONArray jsonArray1 = JSONArray.parseArray(options);
                        String s = "";
                        String string = gameAttribute1.getFieldName().replace("一", "1").replace("二", "2").replace("三", "3")
                                .replace("四", "4").replace("五", "5").replace("六", "6").replace("七", "7")
                                .replace("八", "8").replace("九", "9").replace("十", "10");
                        for (Object p : jsonArray1) {
                            if (JSONObject.parseObject(p.toString()).getString("key").equals(string)) {
                                s = JSONObject.parseObject(p.toString()).getString("value");
                            }
                        }
                        if (StringUtils.isNotEmpty(s)) {
                            optionss += "shishitaoshuliang=" + s;
                        } else {
                            optionss += "shishitaoshuliang=1史诗套";
                        }
                    }
                }
                if (JSONObject.parseObject(o.toString()).getString("propertyname").equals("史诗套")) {
                    if (StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())) {
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF3());
                        String options = JSONObject.parseObject(o.toString()).getString("options");
                        JSONArray jsonArray1 = JSONArray.parseArray(options);
                        String s = "";
                        for (GameAttribute gameAttribute1 : gameAttributeList) {
                            for (Object p : jsonArray1) {
                                if (JSONObject.parseObject(p.toString()).getString("key").equals(gameAttribute1.getFieldName())) {
                                    s = JSONObject.parseObject(p.toString()).getString("value");
                                }
                            }
                        }
                        if (StringUtils.isNotEmpty(s)) {
                            optionss += "|shishitao=" + s;
                        }
                    }
                }
                if (JSONObject.parseObject(o.toString()).getString("propertyname").equals("史诗套皮肤")) {
                    if (StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF5())) {
                        List<GameAttribute> gameAttributeList = getGameAttributs(goodsInfoAndAttrValueDto.getF5());
                        String options = JSONObject.parseObject(o.toString()).getString("options");
                        JSONArray jsonArray1 = JSONArray.parseArray(options);
                        String s = "";
                        for (GameAttribute gameAttribute1 : gameAttributeList) {
                            for (Object p : jsonArray1) {
                                if (JSONObject.parseObject(p.toString()).getString("key").equals(gameAttribute1.getFieldName())) {
                                    s = JSONObject.parseObject(p.toString()).getString("value");
                                }
                            }
                        }
                        if (StringUtils.isNotEmpty(s)) {
                            optionss += "|shishitaopifu=" + s;
                        }
                    }
                }
            }
            params.put("options", optionss);
        }else if ("光遇".equals(goodsInfoAndAttrValueDto.getGameName())) {
            params.put("gameid", "13");
            if(goodsInfoAndAttrValueDto.getSystemName().equals("安卓")){
                if(goodsInfoAndAttrValueDto.getCarrierName().equals("网易手机")){
                    params.put("serverid", 503);
                }else{
                    params.put("serverid", 502);
                }
            }else{
                if(goodsInfoAndAttrValueDto.getCarrierName().equals("网易手机")){
                    params.put("serverid", 507);
                }else{
                    params.put("serverid", 506);
                }
            }
            String optionss = "lazhushuliang=0";
            String response = kuozhanshuxing(13);
            String data = JSONObject.parseObject(response).getString("data");
            JSONArray jsonArray = JSONArray.parseArray(data);
            for(Object o : jsonArray){
                if(JSONObject.parseObject(o.toString()).getString("propertyname").equals("毕业季节")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())){
                        List<GameAttribute> gameAttributs = getGameAttributs(goodsInfoAndAttrValueDto.getF2());
                        String options = JSONObject.parseObject(o.toString()).getString("options");
                        JSONArray jsonArray1 = JSONArray.parseArray(options);
                        String string = "";
                        for(GameAttribute gameAttribute1 : gameAttributs) {
                            for (Object p : jsonArray1) {
                                if (JSONObject.parseObject(p.toString()).getString("key").contains(gameAttribute1.getFieldName())) {
                                    string+=","+JSONObject.parseObject(p.toString()).getString("key");
                                }
                            }
                        }
                        if(StringUtils.isNotEmpty(string)){
                            optionss += "|gyjijie="+string.substring(1);
                        }
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("propertyname").equals("半毕业季节")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())){
                        List<GameAttribute> gameAttributs = getGameAttributs(goodsInfoAndAttrValueDto.getF2());
                        String options = JSONObject.parseObject(o.toString()).getString("options");
                        JSONArray jsonArray1 = JSONArray.parseArray(options);
                        String string = "";
                        for(GameAttribute gameAttribute1 : gameAttributs) {
                            for (Object p : jsonArray1) {
                                if (JSONObject.parseObject(p.toString()).getString("key").contains(gameAttribute1.getFieldName())) {
                                    string+=","+JSONObject.parseObject(p.toString()).getString("key");
                                }
                            }
                        }
                        if(StringUtils.isNotEmpty(string)){
                            optionss += "|gybanjijie="+string.substring(1);
                        }
                    }
                }
                if(JSONObject.parseObject(o.toString()).getString("propertyname").equals("有卡未毕业")){
                    if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())){
                        List<GameAttribute> gameAttributs = getGameAttributs(goodsInfoAndAttrValueDto.getF2());
                        String options = JSONObject.parseObject(o.toString()).getString("options");
                        JSONArray jsonArray1 = JSONArray.parseArray(options);
                        String string = "";
                        for(GameAttribute gameAttribute1 : gameAttributs) {
                            for (Object p : jsonArray1) {
                                if (JSONObject.parseObject(p.toString()).getString("key").contains(gameAttribute1.getFieldName())) {
                                    string+=","+JSONObject.parseObject(p.toString()).getString("key");
                                }
                            }
                        }
                        if(StringUtils.isNotEmpty(string)){
                            optionss += "|gyjijieka="+string.substring(1);
                        }
                    }
                }
            }
            params.put("options", optionss);
        }else{
            throw new GlobalException("暂不支持此游戏:"+goodsInfoAndAttrValueDto.getGameName());
        }
        if(!goodsInfoAndAttrValueDto.getGameName().equals("光遇")) {
            if("王者荣耀,和平精英".contains(goodsInfoAndAttrValueDto.getGameName())) {
                if (gameAttribute.getFieldName().equals("包人脸")) {
                    params.put("smstate", 2);
                } else if (gameAttribute.getFieldName().equals("可二次实名")) {
                    params.put("smstate", 1);
                } else {
                    params.put("smstate", 0);
                }
            }else{
                params.put("smstate", 1);
            }
        }else{
            params.put("smstate", 1);
        }
        return params;
    }

    private List<GameAttribute> getGameAttributs(String ids) {
        List<GameAttribute> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(ids)) {
            list = gameAttributeMapper.selectGameAttributeListByIds((Long[]) ConvertUtils.convert(ids.split(","),
                    Long.class));
        }
        return list;
    }

    private String reIds(String allIds, String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            if (StringUtils.isNotEmpty(allIds)) {
                allIds = allIds + "," + ids;
            } else {
                allIds = ids;
            }
        }
        return allIds;
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, "delete", salePartner);
        Map<String, Object> result = new HashMap<>();
        String response = HttpUtils.doPostForm(salePartner.getOther(), params, null);
        log.info("游戏阁删除接口请求参数={}", JSONObject.toJSONString(params));
        log.info("游戏阁删除接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!Objects.isNull(resultJson)) {
            if (!"true".equals(resultJson.getString("succ"))) {
                result.put("code", 500);
            } else {
                result.put("code", 200);
                ArtRec artRecreq = new ArtRec();
                artRecreq.setConfig(salePartner.getId());
                artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
                if (artRecs.size() > 0) {
                    artRecMapper.deleteArtRecById(artRecs.get(0).getId());
                }
            }
            result.put("msg", resultJson.getString("msg"));
        } else {
            result.put("code", 500);
            result.put("msg", "删除失败");
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);

        return AjaxResult.success(result);
    }

    public static void main(String[] args) {

        /*JSONObject resultJson1 = JSONObject.parseObject(response);
        System.out.println(resultJson1);
        JSONArray array = JSONArray.parseArray(resultJson1.get("data").toString());
        for(Object o : array){
            System.out.println(JSONObject.parseObject(o.toString()).get("propertyname"));
        }*/
        /*JSONObject resultJson1 = JSONObject.parseObject(response);
        JSONArray jsonArray = JSONArray.parseArray(resultJson1.getString("arealist"));
        for (Object o : jsonArray){
            System.out.println(o);
        }*/
        String response = kuozhanshuxing(13);
//        String response = youxiqufu(13);

        System.out.println(response);
    }

    public static String kuozhanshuxing(int type) {
        Map<String, Object> params1 = new TreeMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("content-type", "application/x-www-form-urlencoded");
        params1.put("key", "youxige_ziying_KDNDKKGFEDKG");
        params1.put("merchant_no", "aishangyou");
        params1.put("gameid", type);
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm("http://api.youxige.com/tools/getExpendAttribute.ashx", params1, head);
        return response;
    }

    public static String youxiqufu(int type) {
        Map<String, Object> params1 = new TreeMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("content-type", "application/x-www-form-urlencoded");
        params1.put("key", "youxige_ziying_KDNDKKGFEDKG");
        params1.put("merchant_no", "aishangyou");
        params1.put("gameid", type);
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm("http://api.youxige.com/tools/getAreaServer.ashx", params1, head);
        return response;
    }

}
